<template>
  <div>
    <el-dialog
      :visible.sync="isshow"
      :title="mode === 'edit' ? '修改轮播图界面' : '新增轮播图界面'"
    >
      <el-form
        size="large"
        label-width="100px"
        :model="swiperlist"
        ref="formRef"
      >
        <el-form-item label="上传图片" prop="img">
          <el-upload
            class="avatar-uploader"
            action="/admin/file/upload"
            :show-file-list="true"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <el-dialog v-model="preDialogVisible">
            <img :src="imageUrl" alt="Preview Image" style="width: 100%" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="跳转连接" prop="link">
          <el-input placholder="链接" v-model="swiper.link" />
        </el-form-item>
        <el-form-item label="alt说明" prop="alt">
          <el-input placholder="请输入用户昵称" v-model="swiper.alt" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="swiperlist.status"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="isshow = false">取消</el-button>
          <el-button type="primary" @click="onSubmit"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { addSwiper } from '@/api/swiper'
export default {
  data () {
    return {
      isshow: false,
      swiperlist: {
        link: '',
        alt: '',
        img: '',
        bannerid: '',
        status: 1
      },
      mode: this.$parent.mode,
      rules: [],
      swiper: this.$parent.swiperlist,
      fileList: this.$parent.fileList,
      imageUrl: '',
      preDialogVisible: false
    }
  },
  props: {
    // isshow: {
    //   type: Boolean
    //   // default: true
    // }
  },
  methods: {
    open () {
      this.isshow = true
    },
    async onSubmit () {
      console.log(1, this.$parent.swiperlist)
      this.isshow = false
      delete this.swiperlist.bannerid
      await addSwiper(this.swiperlist)
      // this.$refs.formRef.validate(async valid => {
      //   if (!valid) return
      //   try {
      //     if (this.mode === 'add') {

      //     }
      //   } catch (error) {
      //     console.log(error)
      //   }
      // })
    },
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.swiperlist.img = res.body.fileUrl
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>
<style scoped>
.cover {
  width: 10000px;
  height: 10000px;
  background: rgba(0, 0, 0, 0.7);
  position: absolute;
  top: 0;
  z-index: 99;
}
.box {
  text-align: center;
  width: 500px;
  height: 400px;
  outline: 1px #000 solid;
  position: absolute;
  left: 40%;
  top: 40%;
  background: #fff;
  z-index: 100;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
